package com.axend.aerosense.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.ui.WebActivity;
import com.axend.aerosense.home.adapter.IReportsClick;
import com.axend.aerosense.home.adapter.WardReportAdapter;
import com.axend.aerosense.home.entity.WardReportBean;
import com.axend.aerosense.home.entity.WardReportListBean;
import com.axend.aerosense.home.viewmodel.WardReportViewModel;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.databinding.HomeFragemntReportBinding;

import java.util.ArrayList;
import java.util.List;

public class WardReportListFragment extends MvvmBaseFragment<HomeFragemntReportBinding, WardReportViewModel>
        implements ILoadDataResult<WardReportListBean> {
    private ArrayList<WardReportBean> wardReportBeanArrayList = new ArrayList<>();
    private WardReportAdapter adapter;

    public static WardReportListFragment getInstance() {
        WardReportListFragment fragment = new WardReportListFragment();
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        adapter = new WardReportAdapter();
        viewDataBinding.userList.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.userList.setAdapter(adapter);
        viewDataBinding.homeRefreshLayout.setEnableLoadMore(false);
        viewDataBinding.homeRefreshLayout.setOnRefreshListener(refreshLayout -> {
            viewModel.onLoad();
        });

        adapter.setiReportsClick(new IReportsClick<WardReportBean>() {
            @Override
            public void onAdlReportClick(WardReportBean data) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra(WebActivity.URL, data.getAdlurl());
                intent.putExtra(WebActivity.REPORT_KEY, data.getEncrypt());
                getActivity().startActivity(intent);
            }

            @Override
            public void onSleepReportClick(WardReportBean data) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra(WebActivity.URL, data.getSleepUrl());
                intent.putExtra(WebActivity.REPORT_KEY, data.getEncrypt());
                getActivity().startActivity(intent);
            }
        });

        setLoadSir(viewDataBinding.homeContentView);
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_fragemnt_report;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected WardReportViewModel getViewModel() {
        BaseViewModelFactroy<WardReportListBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        WardReportViewModel tmp = new ViewModelProvider(this, myViewModelFactroy).get(WardReportViewModel.class);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(WardReportListBean wardReportListBean) {
        List<WardReportBean> wardList = wardReportListBean.getWardInfo();
        if (null != wardList && !wardList.isEmpty()){
            showContent();
            wardReportBeanArrayList.clear();
            wardReportBeanArrayList.addAll(wardList);
            adapter.setList(wardList);
            //adapter.notifyDataSetChanged();
            viewDataBinding.homeRefreshLayout.finishLoadMore();
            viewDataBinding.homeRefreshLayout.finishRefresh();
        }else {
            showEmpty();
        }
    }

    @Override
    public void loadDataFail(String decs) {

    }
}
